package com.kinyx.framework.db.sentence.res.demo.delete;

import org.junit.Assert;
import org.junit.Test;

import com.kinyx.framework.db.configs.DBConfigsHelper;
import com.kinyx.framework.db.sentence.CompareType;
import com.kinyx.framework.db.sentence.res.Condition;
import com.kinyx.framework.db.sentence.res.KDELETE;
import com.kinyx.framework.db.sentence.res.KSQLBuilder;
import com.kinyx.test.entities.impl.DemoEntityR;

public class DELETE__Tester {

	final KDELETE delete = KSQLBuilder.delete(DemoEntityR.class);

	String defaultSentenceSQL = "DELETE FROM T_DEMO";

	protected void test(final KDELETE delete, final String expectedSentence, final String expectedString) {
		Assert.assertEquals(expectedSentence, delete.getSentence(DBConfigsHelper.getDBConfigs(null)));
		Assert.assertEquals(expectedString, delete.toString());
	}

	String defaultStringSQL = "SQL: [" + this.defaultSentenceSQL + "], params: []";

	@Test
	public void test0() {
		this.test(this.delete, this.defaultSentenceSQL, this.defaultStringSQL);
	}

	@Test
	public void demo1() {
		final String sentence = "DELETE FROM T_DEMO WHERE ID = ?";
		final String str = "SQL: [" + sentence + "], params: [foo]";
		this.test(//
				KSQLBuilder.delete(DemoEntityR.class)//
						.where(//
								Condition.eq(DemoEntityR.id, "foo")//
						)//
				, sentence, str);
	}

	@Test
	public void demo2() {
		final String sentence = "DELETE FROM T_DEMO WHERE (ID = ?)";
		final String str = "SQL: [" + sentence + "], params: [foo]";
		this.test(//
				KSQLBuilder.delete(DemoEntityR.class)//
						.where(//
								Condition.of(//
										Condition.eq(DemoEntityR.id, "foo")//
								)//
						)//
				, sentence, str);
	}

	@Test
	public void test_where_simple1() {
		final String sentence = this.defaultSentenceSQL + " WHERE ID = ?";
		final String str = "SQL: [" + sentence + "], params: [foo]";
		this.test(this.delete//
				.where(//
						Condition.eq(DemoEntityR.id, "foo")//
				)//
				, sentence, str);
	}

	@Test
	public void test_where_simple2AND() {
		final String sentence = this.defaultSentenceSQL + " WHERE ID = ? AND ID = ?";
		final String str = "SQL: [" + sentence + "], params: [foo, bar]";
		this.test(this.delete//
				.where(//
						Condition.eq(DemoEntityR.id, "foo")//
								.and(DemoEntityR.id, CompareType.EQ, "bar")//
				)//
				, sentence, str);
	}

	@Test
	public void test_where_simple2OR() {
		final String sentence = this.defaultSentenceSQL + " WHERE ID = ? OR ID = ?";
		final String str = "SQL: [" + sentence + "], params: [foo, bar]";
		this.test(this.delete//
				.where(//
						Condition.eq(DemoEntityR.id, "foo")//
								.or(DemoEntityR.id, CompareType.EQ, "bar")//
				)//
				, sentence, str);
	}

	@Test
	public void test_where_complex1() {
		final String sentence = this.defaultSentenceSQL + " WHERE (ID = ?)";
		final String str = "SQL: [" + sentence + "], params: [foo]";
		this.test(this.delete//
				.where(//
						Condition.of(//
								Condition.eq(DemoEntityR.id, "foo")//
						)//
				)//
				, sentence, str);
	}

	@Test
	public void test_where_complex2AND() {
		final String sentence = this.defaultSentenceSQL + " WHERE (ID = ? AND ID = ?)";
		final String str = "SQL: [" + sentence + "], params: [foo, bar]";
		this.test(this.delete//
				.where(//
						Condition.of(//
								Condition.eq(DemoEntityR.id, "foo")//
										.and(DemoEntityR.id, CompareType.EQ, "bar")//
						)//
				)//
				, sentence, str);
	}

	@Test
	public void test_where_complex2OR() {
		final String sentence = this.defaultSentenceSQL + " WHERE (ID = ? OR ID = ?)";
		final String str = "SQL: [" + sentence + "], params: [foo, bar]";
		this.test(this.delete//
				.where(//
						Condition.of(//
								Condition.eq(DemoEntityR.id, "foo")//
										.or(DemoEntityR.id, CompareType.EQ, "bar")//
						)//
				)//
				, sentence, str);
	}

}
